<script setup lang="ts">
import { ref, defineExpose, reactive, defineEmits } from "vue";
import type { FormInstance, FormRules } from "element-plus";
import { regionDataPlus } from "@/utils/chinaArea";

const dialogVisible = ref(false);
const title1 = ref("");
const ruleForm = ref({
  fullName: "",
  shortName: "",
  province: "",
  area: "",
  nuCode: "",
  code: "",
  type: "",
  explain: "",
  contact: "",
  phone: "",
  status: ""
});
const footerShow = ref(false);
const ruleFormRef = ref<FormInstance>();
const init = (row, title) => {
  resetForm(ruleFormRef.value);
  if (row) {
    ruleForm.value = { ...row };
  }

  footerShow.value = title == "单位详情" ? false : true;
  dialogVisible.value = true;
  title1.value = title;
};
const $emit = defineEmits(["submit"]);
defineExpose({ init });

const rules = reactive<FormRules>({
  fullName: [{ required: true, message: "请输入单位全称", trigger: "blur" }],
  shortName: [{ required: true, message: "请输入单位简称", trigger: "blur" }],
  nuCode: [{ required: true, message: "请选择统码", trigger: "blur" }],
  area: [{ required: true, message: "请选择省份", trigger: "blur" }],
  type: [{ required: true, message: "请选择单位类型", trigger: "blur" }],
  code: [{ required: true, message: "请输入单位编码", trigger: "blur" }],
  contact: [{ required: true, message: "请输入联系人", trigger: "blur" }],
  phone: [{ required: true, message: "请输入联系电话", trigger: "blur" }]
});
const submitForm = async (formEl: FormInstance | undefined) => {
  if (!formEl) return;
  await formEl.validate((valid, fields) => {
    if (valid) {
      dialogVisible.value = false;
      $emit("submit", Object.assign({}, ruleForm.value));
      formEl.resetFields();
    } else {
      console.log("error submit!", fields);
    }
  });
};

const resetForm = (formEl: FormInstance | undefined) => {
  console.log(formEl);
  if (!formEl) return;
  formEl.resetFields();
  dialogVisible.value = false;
};
const handleClose = (done: () => void) => {
  done();
};
</script>

<template>
  <el-dialog
    v-model="dialogVisible"
    :title="title1"
    width="900"
    :before-close="handleClose"
  >
    <el-form
      ref="ruleFormRef"
      :model="ruleForm"
      :rules="rules"
      label-width="auto"
      class="demo-ruleForm"
      status-icon
      :disabled="title1 === '单位详情' ? true : false"
    >
      <el-form-item label="单位全称" prop="fullName">
        <el-input
          v-model="ruleForm.fullName"
          placeholder="请输入单位全称"
          clearable
        ></el-input>
      </el-form-item>
      <el-form-item label="单位简称" prop="shortName">
        <el-input
          v-model="ruleForm.shortName"
          placeholder="请输入单位简称"
          clearable
        ></el-input>
      </el-form-item>
      <el-form-item label="省份" prop="area">
        <el-cascader
          placeholder="请选择省份"
          v-model="ruleForm.area"
          :options="regionDataPlus"
          class="w-full"
        />
      </el-form-item>
      <el-form-item label="统码" prop="nuCode">
        <el-input
          v-model="ruleForm.nuCode"
          placeholder="请输入统码"
          clearable
        ></el-input>
      </el-form-item>
      <el-form-item label="单位类型" prop="type">
        <el-select v-model="ruleForm.type" placeholder="请选择单位类型">
          <el-option label="公益组织" value="公益组织"></el-option>
          <el-option label="三方代理" value="三方代理"></el-option>
          <el-option label="社会组织" value="社会组织"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="组织编码" prop="code">
        <el-input
          v-model="ruleForm.code"
          placeholder="请输入组织编码"
          clearable
        ></el-input>
      </el-form-item>
      <el-form-item label="组织说明" prop="explain" class="w-full">
        <el-input
          v-model="ruleForm.explain"
          placeholder="请输入组织说明"
          :autosize="{ minRows: 2, maxRows: 4 }"
          type="textarea"
          clearable
        ></el-input>
      </el-form-item>
      <el-form-item label="联系人" prop="contact">
        <el-input
          v-model="ruleForm.contact"
          placeholder="请输入联系人"
          clearable
        ></el-input>
      </el-form-item>
      <el-form-item label="联系电话" prop="phone">
        <el-input
          v-model="ruleForm.phone"
          placeholder="请输入联系电话"
          clearable
        ></el-input>
      </el-form-item>
      <el-form-item label="是否启用">
        <el-switch
          v-model="ruleForm.status"
          active-value="1"
          inactive-value="0"
        />
      </el-form-item>
    </el-form>
    <template #footer v-if="footerShow">
      <div class="dialog-footer">
        <el-button @click="resetForm(ruleFormRef)">取消</el-button>
        <el-button type="primary" @click="submitForm(ruleFormRef)">
          确认
        </el-button>
      </div>
    </template>
  </el-dialog>
</template>

<style lang="scss" scoped>
.demo-ruleForm {
  display: flex;
  flex-wrap: wrap;
  flex-direction: row;
  justify-content: space-between;
  .el-form-item {
    width: 48%;
    &.w-full {
      width: 100%;
    }
  }
}
</style>
